//
//  ViewController.m
//  抽屉效果
//
//  Created by hyl on 16/6/23.
//  Copyright © 2016年 hyl. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic,weak) UIView *mainView;
@property (nonatomic,weak) UIView *leftView;
@property (nonatomic,weak) UIView *rightView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setChildView];
    [self setPan];
    //[self setListenPropery];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -delegateListenPropery
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    NSLog(@"%@",object);
}


#pragma mark - setLisenPropery
-(void) setListenPropery{
    [self.mainView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
}


#pragma mark - setPan
-(void) setPan{
    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    [self.mainView addGestureRecognizer:pan];
}

//-(void) pan:(UIPanGestureRecognizer *) pan{
//    //NSLog(@"%s",__func__);
//    CGPoint p=[pan locationInView:self.view];
//    self.rightView.transform=CGAffineTransformTranslate(self.rightView.transform, p.x, 0);
//    [pan setTranslation:CGPointZero inView:self.rightView];
//    
//}
-(void) pan:(UIPanGestureRecognizer *) pan{
    //NSLog(@"%s",__func__);
    CGPoint transP=[pan translationInView:self.mainView];
    //NSLog(@"%@",NSStringFromCGPoint(transP));
    //NSLog(@"%@",NSStringFromCGRect(self.mainView.frame));
    if (self.mainView.frame.origin.x<0) {
        self.rightView.hidden=YES;
        self.leftView.hidden=NO;
    }else{
        self.rightView.hidden=NO;
        self.leftView.hidden=YES;
    }
    self.mainView.transform=CGAffineTransformTranslate(self.mainView.transform, transP.x, 0);
    [pan setTranslation:CGPointZero inView:self.mainView];
    
}
#pragma mark - setChildView
- (void) setChildView{
    
    UIView *leftView=[[UIView alloc]initWithFrame:self.view.bounds];
    leftView.backgroundColor=[UIColor yellowColor];
    self.leftView=leftView;
    [self.view addSubview:self.leftView];
    
    UIView *rightView=[[UIView alloc]initWithFrame:self.view.bounds];
    rightView.backgroundColor=[UIColor greenColor];
    self.rightView=rightView;
    [self.view addSubview:self.rightView];
    
    UIView *mainView=[[UIView alloc]initWithFrame:self.view.bounds];
    mainView.backgroundColor=[UIColor redColor];
    [self.view addSubview:mainView];
    self.mainView=mainView;
}
@end
